package com.hxalex.simulationhandle.ui.base;

import android.annotation.TargetApi;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.bumptech.glide.Glide;
import com.hxalex.simulationhandle.R;
import com.hxalex.simulationhandle.ui.base.presenter.ClipboardCheckPresenter;
import com.hxalex.simulationhandle.ui.base.presenter.TitlePresenter;
import com.readystatesoftware.systembartint.SystemBarTintManager;


/**
 * Activity 基类
 */
public class BaseActivity extends AppCompatActivity {
    private TitlePresenter mTitlePresenter;
    private SystemBarTintManager mTintManager;
    private ClipboardCheckPresenter mClipboardCheckPresenter;
    private int statusBarResource;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (hasInitWindow()) {
            initWindow();
        }

        //防止魅族切换时黑屏
        setTheme(R.style.AppTheme_Translucent);

        mClipboardCheckPresenter = new ClipboardCheckPresenter(this);
    }

    public boolean hasInitWindow() {
        return true;
    }

    public int getStatusBarResource() {
        if(statusBarResource==0) return R.color.toolbar_background;
        return statusBarResource;
    }

    public  void setStatusBarResource(int statusBarResource)
    {
        this.statusBarResource=statusBarResource;
    }
    @TargetApi(19)
    private void initWindow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            mTintManager = new SystemBarTintManager(this);
            mTintManager.setStatusBarTintResource(getStatusBarResource());
            mTintManager.setStatusBarTintEnabled(true);
        }
    }

    protected boolean canSwipeDismiss() {
        return true;
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        if (canSwipeDismiss()) {
            //TODO
//            SwipeBackHelper.onCreate(this);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mClipboardCheckPresenter.checkText((ViewGroup) getWindow().getDecorView());
    }

    public View initToolBar() {
        return initToolBar(true);
    }

    public View initToolBar(boolean disPlayHomeAsUp) {
        View view = findViewById(R.id.rl_title);
        mTitlePresenter = new TitlePresenter(view, disPlayHomeAsUp, v -> OnBackpresseed());
        return view;
    }

    public void OnBackpresseed() {
        finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        Glide.with(this).onLowMemory();
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        Glide.with(this).onTrimMemory(level);
    }

    /**
     * 初始化toolbar及右menu的内容和点击事件监听
     *
     * @param rightText         toolbar 右菜单文字
     * @param righClickListener 右菜单点击事件
     * @return
     */
    public void initToolBarWithRightMenu(String rightText, View.OnClickListener righClickListener) {
        initToolBar();
        mTitlePresenter.initToolBarRightMenu(rightText, righClickListener);
    }

    /**
     * 初始化toolbar及右menu的内容和点击事件监听
     *
     * @param rightDrawable     toolbar 右菜单图片
     * @param righClickListener 右菜单点击事件
     * @return
     */
    public void initToolBarWithRightMenu(Drawable rightDrawable, View.OnClickListener righClickListener) {
        initToolBar();
        mTitlePresenter.initToolBarRightMenu(rightDrawable, righClickListener);
    }

    public void setToolBarRightMenuVisibility(boolean visible) {
        if (mTitlePresenter != null) {
            mTitlePresenter.setToolBarRightMenuVisibility(visible);
        }
    }

    @Override
    protected void onDestroy() {
    
        super.onDestroy();
    }
}